//养护基地
var vm = new Vue({
	el: '#app',
	data: function() {
		return {
			facility: [{ //模拟设备数量信息
				"ID": "0",
				"type": "facility",
				"name": "高空作业车",
				"checked": true
			}, {
				"ID": "1",
				"type": "facility",
				"name": "多功能防护车",
				"checked": true
			}, {
				"ID": "2",
				"type": "facility",
				"name": "防撞车",
				"checked": false
			}, {
				"ID": "3",
				"type": "facility",
				"name": "抢险车",
				"checked": false
			}],
			materiel: [{ //模拟物资数量信息
				"ID": "0",
				"type": "materiel",
				"name": "反光背心",
				"checked": true
			}, {
				"ID": "1",
				"type": "materiel",
				"name": "警示灯",
				"checked": true
			}, {
				"ID": "2",
				"type": "materiel",
				"name": "工业盐",
				"checked": false
			}]
		}
	},
	mounted: function() {
		var that = this;
		mui.init({});
		that.getData(); //获取数据
		mui.ready(function() {

		})
	},
	methods: {
		//获取数据
		getData: function() {
			var that = this;
			//请求数据↓↓↓↓
            $.getJSON('/getBaseFacilitys',{},function(res){
                that.facility=res.facility;
                that.materiel=res.materiel;
            })
		},
		//添加
		addRow: function(name) {
			var that = this;
			switch(name) {
				case 'facility':
					that.facility.push({
						"id": that.facility.length,
						"type": "facility",
						"name": "",
						"checked": false
					})
					break;
				case 'materiel':
					that.materiel.push({
						"id": that.materiel.length,
						"type": "materiel",
						"name": "",
						"checked": false
					})
					break;

			}
		},
		//删除
		deletRow: function(name, index) {
			var that = this;
			mui.confirm('确认删除该字段？', '提示', ['取消', '确定'], function(res) {
				if(res.index == 1) {
					switch(name) {
						case 'facility':
							that.facility.splice(index, 1);
							break;
						case 'materiel':
							that.materiel.splice(index, 1);
							break;
					}
				}
			})

		},
		//选中
		choseItem: function(name, index) {
			var that = this;
			var _this = $(event.target);
			var flag = false;
			if(_this.prop('checked')) {
				flag = true
			}
			switch(name) {
				case 'facility':
					that.facility[index].checked = flag;
					break;
				case 'materiel':
					that.materiel[index].checked = flag;
					break;
			}
		},
		//保存
		save: function() {
			var that = this;
			var facilityArr = that.facility; //设备数目集合
			var materielArr = that.materiel; //物资数目集合

			console.log(facilityArr)
			console.log(facilityArr)
            $.post('/createMaintainBase', {
                "facility":JSON.stringify(facilityArr),
                "materiel": JSON.stringify(materielArr)
            }, function () {
                window.location.reload()
            })
		}
	}
})